Ext.define('Isms.controller.NewsController', {
	extend : 'Ext.app.Controller',
	stores : ['NewsStore'],
	models : [],
	views : ['news-post.SingleNews', 'news-post.PostNews',
			'news-post.NewsList', 'Ext.window.MessageBox', 'Ext.tip.*'],
	init : function() {
		var vNewsAdd = null;
		this.control({
			'news-list>grid' : {
				render : function() {
					var grid = Ext.getCmp('news_grid');
					grid.store.loadPage(1);
				}
				// itemclick : this.newsItemClick
			},
			'news-list button[action = news_add_action]' : {
				click : function(btn) {
					if (!vNewsAdd) {
						vNewsAdd = Ext.create('widget.window', {
									title : '发布消息/公告',
									closable : true,
									closeAction : 'hide',
									width : 600,
									minWidth : 350,
									height : 350,
									items : [{
												xtype : 'news-post'
											}]
								});
					}
					vNewsAdd.show();
				}
			},
			'news-list button[action = news_del_action]' : {
				click : function(btn) {
					Ext.MessageBox.confirm('警告！！', '确定删除？？？？', function(btn) {
						if (btn == "yes") {
							var mFile_list = Ext.getCmp('news_grid');
							var mDatas = mFile_list.getSelectionModel()
									.getSelection();
							var params = {};
							params.id = new Array();
							for (var i = 0; i < mDatas.length; i++) {
								params.id[i] = mDatas[i].get('id');
							}
							Ext.Ajax.request({
										url : '../news/delNews.do',
										params : params,
										method : 'get',// 请求方式
										success : function(respsonse) {
											Ext.Msg.alert('提示', '删除成功',
													function() {
														// 重新加载数据
														Ext.getCmp('news_grid').store
																.loadPage(1);
													});

										},
										failure : function(respsonse) {
											showMessage(response);
										}
									});

						}
					});
				}
			},
			'news-post button[action = news_post_action]' : {
				click : this.news_add
			},
			'news-list button[action = news_edit_action]' : {
				click : this.showEditPanel
			},
			'single-news button[action = news_edit]' : {
				click : this.handelEdit
			},
			'single-news button[action = closeEditPanel]' : {
				click : function(btn){
					vNewsEdit.close();
				}
			}
		});
	},
	handelEdit : function(btn) {
		var form = btn.up('form').getForm();
		if (form.isValid()) {
			form.submit({
						url : '../news/edit.do',
						params : form.getValues(true),
						method : 'POST',
						waitTitle : '请稍后',
						waitMsg : '正在上传文档文件 ...',
						success : function(form, action) {
							Ext.MessageBox.alert("提示", "操作成功！", function() {
										// 重新加载数据
										Ext.getCmp('news_grid').store
												.loadPage(1);
										// 关闭窗口
										vNewsEdit.close();
									});
						},
						failure : function(form, action) {
							Ext.MessageBox.alert("错误！",
									"操作失败<br>服务报告了一项错误，或连接失败");
						}
					});
		}
	},
	showEditPanel : function(btn) {
		var rec = Ext.getCmp('news_grid').getSelectionModel().getLastSelected();
		console.log(rec);
		var obj = Ext.getCmp('panel_newslist');
		if (!vNewsEdit) {
			vNewsEdit = Ext.create('widget.window', {
						title : '发布资料文档',
						closable : true,
						closeAction : 'hide',
						width : 600,
						minWidth : 350,
						height : 350,
						items : [{
									xtype : 'single-news'
								}]
					});
		}
		Ext.getCmp('n_id').setValue(rec.get('id'));
		Ext.getCmp('n_title').setValue(rec.get('title'));
		if (rec.get('type'))
			Ext.getCmp('n_type_true').setValue(true);
		else
			Ext.getCmp('n_type_false').setValue(true);
		if (rec.get('isStickie'))
			Ext.getCmp('n_isStickie_true').setValue(true);
		else
			Ext.getCmp('n_isStickie_false').setValue(true);
		Ext.getCmp('n_content').setValue(rec.get('content'));
		
		vNewsEdit.show();
	},
	news_add : function(obj) {
		if (obj.text == '添加') {
			var form = obj.up('form').getForm();
			if (form.isValid()) {
				form.submit({
							url : '../news/addNews.do',
							params : form.getValues(true),
							method : 'POST',
							// waitTitle : '请稍后',
							// waitMsg : '正在添加中……………',
							success : function() {
								Ext.MessageBox.alert("提示", "操作成功！", function() {
											vNewsAdd.close();
											var grid = Ext.getCmp('news_grid');
											grid.store.loadPage(1);
										});
							},
							failure : function(form, action) {
								Ext.MessageBox.alert("错误！",
										"操作失败<br>服务报告了一项错误，或连接失败");
							}
						});
			}
		};
	},
	newsItemClick : function(grid, record, item, index, e, eOpts) {
		// var itemdata=record.get
		var singleNews = Ext.create('widget.single-news');
		Ext.getCmp('vTitle').setValue(record.get('title'));
		Ext.getCmp('vContent').setValue(record.get('content'));
		var top = "否";
		if (record.get('cancle') == true)
			top = "是";
		Ext.getCmp('vTop').setValue(top);
		Ext.getCmp('vTitle').setValue(record.get('title'));
		var win = Ext.create('widget.window', {
					title : '查看文档',
					closable : true,
					closeAction : 'hide',
					width : 600,
					minWidth : 350,
					height : 350,
					items : [{
								xtype : singleNews
							}]
				});
		win.show();
	}
});
var vNewsEdit = null;
function showMessage(response) {
	var obj = Ext.util.JSON.decode(response.responseText);
	Ext.Msg.alert('提示', obj.message);
};